package com.cjm.one.goods.apps;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.utils.AuthCustomerContextHolder;
import com.cjm.one.goods.service.GoodsCollectService;
import com.cjm.one.goods.service.GoodsSkuService;
import com.cjm.one.model.custom.entity.CustomerInfo;
import com.cjm.one.model.goods.entity.GoodsSku;
import com.cjm.one.model.goods.vo.GoodsSkuVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/app/goods/collect")
@Tag(name = "商品收藏", description = "商品收藏")
public class AppGoodsCollectController {

    @Resource
    private GoodsCollectService goodsCollectService;
    @Resource
    private GoodsSkuService goodsSkuService;

    /**
     * @description: 查询当前用户收藏的商品列表
     * @author: cjm
     * @date: 2025/3/12 21:18
     * @param: [current, size]
     * @return: com.cjm.one.common.result.Result<com.cjm.one.model.goods.entity.GoodsInfo>
     **/
    @GetMapping("/list")
    @Operation(summary = "查询当前用户收藏的商品列表")
    public Result<Page<GoodsSkuVo>> getGoodsInfo(@RequestParam(name = "current",defaultValue = "1") Long current, @RequestParam(name = "size",defaultValue = "10") Long size){
        Long id = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        Page<GoodsSkuVo> page =  goodsSkuService.pageListByCollection(id, current, size);
        return  Result.ok(page);
    }
    @GetMapping("/add")
    @Operation(summary = "添加商品收藏")
    public Result<Boolean> insertCollect(@RequestParam("goodsId") Long goodsId) {
        CustomerInfo authContextCustomer = AuthCustomerContextHolder.getAuthContextCustomer();
        log.info("用户id:{},商品id:{}", authContextCustomer.getId(), goodsId);
        if (authContextCustomer == null) {
            return Result.ok(false);
        }
        Long id = authContextCustomer.getId();
        log.info("用户id:{},商品id:{}", id, goodsId);
        Boolean flag =  goodsCollectService.insertCollect(id, goodsId);
        return Result.ok(flag);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除商品收藏")
    public Result<Boolean> deleteCollect(@RequestParam("id") Long id) {
        Long customerId = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        log.info("用户id:{},商品id:{}", customerId, id);
        Boolean flag =  goodsCollectService.deleteCollect(customerId, id);
        return Result.ok(flag);
    }
    @DeleteMapping("/delete/batch")
    @Operation(summary = "批量删除商品收藏")
    public Result<Boolean> deleteCollectBatch(@RequestParam("ids") Long[] ids) {
        Long id = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        log.info("用户id:{},id:{}", id, ids);

        return Result.ok(goodsCollectService.deleteCollectBatch(id, ids));
    }
    @GetMapping("/check/sku/{id}")
    @Operation(summary = "检查商品是否收藏")
    public Result<Boolean> checkGoodsCollect(@PathVariable("id") Long id) {
        CustomerInfo authContextCustomer = AuthCustomerContextHolder.getAuthContextCustomer();

        if (authContextCustomer == null) {
             return Result.ok(false);
        }
          Long customerId = authContextCustomer.getId();
        log.info("用户id:{},商品id:{}", customerId, id);
        return Result.ok(goodsCollectService.checkGoodsCollect(customerId, id));
    }
}
